<?php if (!defined("_ECRIRE_INC_VERSION")) return;

/**
 *  Dymanic Tag wich find and outputs search results
 *  Format: #PDK_RECHERCHE
 */
 
require_once(_DIR_PLUGIN_PDK.'classes/PdkRechercher.php');

function balise_PDK_RECHERCHE($p) {	
	return calculer_balise_dynamique($p,'PDK_RECHERCHE', array());
}

function balise_PDK_RECHERCHE_stat($args, $filtres) {    
	return $args;
}

function balise_PDK_RECHERCHE_dyn() {  
    
    $rechercher = new PdkRechercher();
    
    $found_ids = array(); // Array of found entities IDs
    
    // Articles
    $where = array();
    $where[] = "statut='publie'";
    $found_ids['articles_ids'] = $rechercher->GetSearchIDs(_request('recherche'), 'articles', 'id_article',false, $where);
    
    // Rubriques
    $where = array();    
    $where[] = "statut='publie'";
    $found_ids['rubriques_ids'] = $rechercher->GetSearchIDs(_request('recherche'), 'rubriques', 'id_rubrique',false, $where);    
        
    $additional_tables = pipeline("pdk_recherche", array());
    foreach ($additional_tables as $table_info)
    {
        $ids = $rechercher->GetSearchIDs(_request('recherche'), $table_info['id_table'], $table_info['id_field'], $table_info['fields'], $table_info['where']);
        if (isset($table_info['original_table']) && $table_info['original_table']) {            
            foreach ($ids as $id) {
                if (!in_array($id, $found_ids[$table_info['original_table'].'_ids']))
                    $found_ids[$table_info['original_table'].'_ids'][] = $id; 
            }
        } else {
            $found_ids[$table_info['id_table'].'_ids'] = $ids;
        }
    }  
        
    return array('balise/pdk_recherche', 0, $found_ids);
}

?>